package com.share.netty.splitpacket;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;

/**
 * @author caifan
 * @createTime 2020/5/5
 * @desc:
 */
public class PacketClientHandler extends ChannelInboundHandlerAdapter {
    /**
     * 客户端连接完服务器触发该方法
     * @param ctx
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        /**
         * 测试粘包 半包程序
         */
        /**for (int i=0;i<200;i++) {
            String msg = "netty从入门到放弃,java从入门到精通\n";
            ByteBuf byteBuf = ctx.alloc().buffer();
            byteBuf.writeBytes(msg.getBytes(CharsetUtil.UTF_8));
            ctx.channel().writeAndFlush(msg);
        }*/

        for (int i = 0; i<200; i++) {
            String msg = "netty从入门到放弃";
            //创建协议对象包
            MyMessageProtocol myMessageProtocol = new MyMessageProtocol();
            myMessageProtocol.setLen(msg.getBytes(CharsetUtil.UTF_8).length);
            myMessageProtocol.setContent(msg.getBytes(CharsetUtil.UTF_8));
            ctx.writeAndFlush(myMessageProtocol);
        }
    }
}
